package com.lzc.tlmail.tlmailcoupon.service.impl;

import com.lzc.common.to.MemberPrice;
import com.lzc.common.to.SkuDiscount;
import com.lzc.tlmail.tlmailcoupon.entity.MemberPriceEntity;
import com.lzc.tlmail.tlmailcoupon.entity.SkuLadderEntity;
import com.lzc.tlmail.tlmailcoupon.service.MemberPriceService;
import com.lzc.tlmail.tlmailcoupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzc.common.utils.PageUtils;
import com.lzc.common.utils.Query;

import com.lzc.tlmail.tlmailcoupon.dao.SkuFullReductionDao;
import com.lzc.tlmail.tlmailcoupon.entity.SkuFullReductionEntity;
import com.lzc.tlmail.tlmailcoupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;
    @Autowired
    private MemberPriceService memberPriceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存sku关联的优惠信息 包括满减 满多少折扣 会员优惠
     * @param skuDiscount
     */
    @Override
    public void saveDiscount(SkuDiscount skuDiscount) {
        //6.4 sku tlmail_sms库满减sms_sku_full_reduction表   满几件打几折sms_sku_ladder 会员折扣sms_member_price
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuDiscount,skuFullReductionEntity);
        skuFullReductionEntity.setAddOther(skuDiscount.getPriceStatus());
        if(skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0"))==1){
            this.save(skuFullReductionEntity);
        }


        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(skuDiscount,skuLadderEntity);
        skuLadderEntity.setAddOther(skuDiscount.getCountStatus());
        if(skuLadderEntity.getFullCount()>0){
            skuLadderService.save(skuLadderEntity);
        }


        List<MemberPriceEntity> memberPriceEntities = skuDiscount.getMemberPrice().stream().map(memberPrice -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuDiscount.getSkuId());
            memberPriceEntity.setMemberLevelId(memberPrice.getId());
            memberPriceEntity.setMemberLevelName(memberPrice.getName());
            memberPriceEntity.setMemberPrice(memberPrice.getPrice());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).filter(entity->{
            return entity.getMemberPrice().compareTo(new BigDecimal("0"))==1;
        }).collect(Collectors.toList());

        memberPriceService.saveBatch(memberPriceEntities);


    }

}